package com.example.controller;

import com.example.entity.User;
import com.example.interceptor.TokenNotValidation;
import com.example.service.UserService;
import com.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 获取用户基础信息
     *
     * @param id
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getUserBasicsInfo/{id}")
    public User getUserBasicsInfo(@PathVariable Integer id) {
        return userService.getUserBasicsInfo(id);
    }


    /**
     * 判断是否是用户
     *
     * @return
     */
    @TokenNotValidation
    @GetMapping("/isUserHimself")
    public Boolean isDoctorHimself(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.substring(7);
        Integer userId = JwtUtil.getUserId(token);
        User user = userService.selectByPrimaryKey(userId);
        if (user.getRoleId() == 2) {
            return true;
        } else {
            return false;
        }
    }
}
